package com.zyp.messageboard.controller.filter;

import com.zyp.messageboard.util.SensitiveWordUtil;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebFilter("/addMessage")
public class SensitiveWordFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String content = httpRequest.getParameter("content");

        // 替换敏感词汇
        String filteredContent = SensitiveWordUtil.replaceSensitiveWords(content);
        httpRequest.setAttribute("filteredContent", filteredContent);

        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // Initialization code, if needed
    }

    @Override
    public void destroy() {
        // Cleanup code, if needed
    }
}
